package team.scau.laboratory_repair.web.sys;

import com.querydsl.core.QueryResults;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import team.scau.laboratory_repair.common.base.BaseController;
import team.scau.laboratory_repair.common.json.JqGridResult;
import team.scau.laboratory_repair.common.page.MyPage;
import team.scau.laboratory_repair.modules.po.QUser;
import team.scau.laboratory_repair.modules.po.User;
import team.scau.laboratory_repair.service.RoleService;
import team.scau.laboratory_repair.service.UserService;

import java.util.HashMap;
import java.util.Map;


/**
 * @author 30254
 * creadtedate:2018/8/19
 */
@Controller
@CommonsLog
@RequestMapping("/pc/sys/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @ModelAttribute("user")
    public User get(@RequestParam(required = false, value = "id") Long id) throws Exception {
        if (id != null && id > 0) {
            return userService.findById(id);
        } else {
            return new User();
        }
    }

    @RequiresPermissions("sys:user:list")
    @GetMapping(value = "/list", produces = "text/html;charset=utf-8")
    public String list(Model model) throws Exception{
        return "/pc/sys/user/list";
    }

    @ResponseBody
    @RequiresPermissions("sys:user:list")
    @RequestMapping(value = "/list.json", produces = "application/json;charset=utf-8")
    public JqGridResult list(@ModelAttribute("user") User user) throws Exception {
        MyPage page = getPage(QUser.class);
        QueryResults<User> list = userService.listOrByExample(user, page);
        JqGridResult result = new JqGridResult(list);
        return result;
    }

    @RequiresPermissions("sys:user:edit")
    @GetMapping(value = "/edit", produces = "text/html;charset=utf-8")
    public String edit(@ModelAttribute("user") User user, Model model) throws Exception{
        model.addAttribute("user",user);
        model.addAttribute("roles",roleService.findAll());
        return "/pc/sys/user/edit";
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     * @throws Exception
     */
    @RequiresPermissions("sys:user:edit")
    @PostMapping(value = "/edit", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute("user") User user){
        Map<String,Object> result = new HashedMap();
        try {
            userService.save(user);
            result.put("status",200);
            result.put("msg","用户信息保存成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.put("status",500);
            result.put("msg","用户信息保存失败");
        }
        return result;
    }

    //@RequiresPermissions("sys:user:editPw")
    @GetMapping(value = "/editPw", produces = "text/html;charset=utf-8")
    public String editPw(@ModelAttribute("user") User user, Model model) throws Exception{
        model.addAttribute("user",user);
        return "/pc/sys/user/editPw";
    }

    /**
     * 修改用户密码
     * @param oldPw
     * @param newPw
     * @param repeatPw
     * @return
     * @throws Exception
     */
    //@RequiresPermissions("sys:user:editPw")
    @PostMapping(value = "/editPw", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> editPw(String oldPw, String newPw, String repeatPw){
        Map<String,Object> result = new HashMap<>();
        try {
            User cUser = getCUser();
            userService.updatePw(cUser,oldPw,newPw,repeatPw);
            SecurityUtils.getSubject().logout();
            result.put("status",200);
            result.put("msg","用户密码修改成功，请重新登录");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.put("status",500);
            result.put("msg","个人密码更新失败，" + e.getMessage());
        }
        return result;
    }

    /**
     * 删除用户
     * @param id
     * @return
     * @throws Exception
     */
    @RequiresPermissions("sys:user:delete")
    @PostMapping(value = "/delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> result = new HashMap<>();
        try {
            if(id != null && id > 0){
                userService.deleteById(id);
                result.put("status",200);
                result.put("msg","删除用户成功");
            }else {
                result.put("status",500);
                result.put("msg","请选择删除的用户");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.put("status",500);
            result.put("msg","删除用户失败");
        }
        return result;
    }

}
